package me.luger.core.mvc;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.mvc.entity.RequestEntity;
import me.luger.core.mvc.filter.pagecache.annotation.PageCache;
import me.luger.core.mvc.log.access.AccessLogEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * WebService
 *
 * @author luger
 * @date 2016/8/18 0018
 */
public class WebService {
    private static Properties properties = new Properties();
    private static ConcurrentHashMap<String, Object> pageCacheMapping = new ConcurrentHashMap<String, Object>();
    private static ConcurrentHashMap<String, PageCache> pageCacheUrlMapping = new ConcurrentHashMap<String, PageCache>();
    private static List<RequestEntity> urlMappingList = Collections.synchronizedList( new ArrayList<RequestEntity>());
    private static List<String> requestUrlList = Collections.synchronizedList(new ArrayList<String>());

    private static ThreadLocal<AccessLogEntity> accessLogEntityThreadLocal = new ThreadLocal<AccessLogEntity>();

    public static ConcurrentHashMap<String, PageCache> getPageCacheUrlMapping() {
        return pageCacheUrlMapping;
    }

    private static String projectName = "";
    private static String projectBasePath = "/";
    private static String projectDomain = "";
    private static String profileId = "undefined";

    public static void putPageCacheUrlMapping(String url, PageCache pageCache) {
        WebService.pageCacheUrlMapping.put(url, pageCache);
    }

    public static List<RequestEntity> getUrlMappingList() {
        return urlMappingList;
    }

    public static List<String> getRequestUrlList() {
        return requestUrlList;
    }

    public static void putUrlMapping(RequestEntity requestEntity){
        WebService.requestUrlList.add(requestEntity.getUrl());
        WebService.urlMappingList.add(requestEntity);
    }

    public static ConcurrentHashMap<String, Object> getPageCacheMapping() {
        return pageCacheMapping;
    }

    public static void setPageCacheMapping(ConcurrentHashMap<String, Object> pageCacheMapping) {
        WebService.pageCacheMapping = pageCacheMapping;
    }

    public static void putPageCache(String key, Object cacheManager) {
        WebService.pageCacheMapping.put(key, cacheManager);
    }

    public static Object getPageCache(String key) {
        return WebService.pageCacheMapping.get(key);
    }

    public static AccessLogEntity getAccessLogEntity(){
        return accessLogEntityThreadLocal.get();
    }

    public static void setAccessLogEntity(AccessLogEntity accessLogEntity){
        accessLogEntityThreadLocal.set(accessLogEntity);
    }

    public static void clean() {
        accessLogEntityThreadLocal.remove();
        LogUtil.cleanMDC();
    }

    public void setProperty(String key,Object value){
        properties.put(key,value);
    }
    public static Object getPropertyAsObj(String key){
        return properties.get(key);
    }

    public static String getProperty(String key){
        Object obj = getPropertyAsObj(key);
        return null == obj ? "" : obj.toString();
    }
    public static Integer getPropertyAsInt(String key){
        Object obj = getPropertyAsObj(key);
        return null == obj ? -1 : Integer.parseInt(obj.toString());
    }

    public static Properties getProperties() {
        return properties;
    }

    public static void setProperties(Properties properties) {
        WebService.properties = properties;
    }

    public static String getProjectName() {
        return projectName;
    }

    public static void setProjectName(String projectName) {
        WebService.projectName = projectName;
    }

    public static String getProjectBasePath() {
        return projectBasePath;
    }

    public static void setProjectBasePath(String projectBasePath) {
        WebService.projectBasePath = projectBasePath;
    }

    public static String getProjectDomain() {
        return projectDomain;
    }

    public static void setProjectDomain(String projectDomain) {
        WebService.projectDomain = projectDomain;
    }

    public static String getProfileId() {
        return profileId;
    }

    public static void setProfileId(String profileId) {
        WebService.profileId = profileId;
    }
}
